package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configCustomProcessBaseUserRelRouter(router *httprouter.Router) {
	router.GET("/customprocessbaseuserrel", GetAllCustomProcessBaseUserRel)
	router.POST("/customprocessbaseuserrel", AddCustomProcessBaseUserRel)
	router.GET("/customprocessbaseuserrel/:argBaseID/:argUserID", GetCustomProcessBaseUserRel)
	router.PUT("/customprocessbaseuserrel/:argBaseID/:argUserID", UpdateCustomProcessBaseUserRel)
	router.DELETE("/customprocessbaseuserrel/:argBaseID/:argUserID", DeleteCustomProcessBaseUserRel)
}

func configGinCustomProcessBaseUserRelRouter(router gin.IRoutes) {
	router.GET("/customprocessbaseuserrel", ConverHttprouterToGin(GetAllCustomProcessBaseUserRel))
	router.POST("/customprocessbaseuserrel", ConverHttprouterToGin(AddCustomProcessBaseUserRel))
	router.GET("/customprocessbaseuserrel/:argBaseID/:argUserID", ConverHttprouterToGin(GetCustomProcessBaseUserRel))
	router.PUT("/customprocessbaseuserrel/:argBaseID/:argUserID", ConverHttprouterToGin(UpdateCustomProcessBaseUserRel))
	router.DELETE("/customprocessbaseuserrel/:argBaseID/:argUserID", ConverHttprouterToGin(DeleteCustomProcessBaseUserRel))
}

// GetAllCustomProcessBaseUserRel is a function to get a slice of record(s) from custom_process_base_user_rel table in the custom_process database
// @Summary Get list of CustomProcessBaseUserRel
// @Tags CustomProcessBaseUserRel
// @Description GetAllCustomProcessBaseUserRel is a handler to get a slice of record(s) from custom_process_base_user_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.CustomProcessBaseUserRel}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbaseuserrel [get]
// http "http://localhost:8080/customprocessbaseuserrel?page=0&pagesize=20" X-Api-User:user123
func GetAllCustomProcessBaseUserRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "custom_process_base_user_rel", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllCustomProcessBaseUserRel(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetCustomProcessBaseUserRel is a function to get a single record from the custom_process_base_user_rel table in the custom_process database
// @Summary Get record from table CustomProcessBaseUserRel by  argBaseID  argUserID
// @Tags CustomProcessBaseUserRel
// @ID argBaseID
// @ID argUserID
// @Description GetCustomProcessBaseUserRel is a function to get a single record from the custom_process_base_user_rel table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"
// @Param  argUserID path int64 true "user_id"
// @Success 200 {object} model.CustomProcessBaseUserRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /customprocessbaseuserrel/{argBaseID}/{argUserID} [get]
// http "http://localhost:8080/customprocessbaseuserrel/1/1" X-Api-User:user123
func GetCustomProcessBaseUserRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argUserID, err := parseInt64(ps, "argUserID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_user_rel", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetCustomProcessBaseUserRel(ctx, argBaseID, argUserID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddCustomProcessBaseUserRel add to add a single record to custom_process_base_user_rel table in the custom_process database
// @Summary Add an record to custom_process_base_user_rel table
// @Description add to add a single record to custom_process_base_user_rel table in the custom_process database
// @Tags CustomProcessBaseUserRel
// @Accept  json
// @Produce  json
// @Param CustomProcessBaseUserRel body model.CustomProcessBaseUserRel true "Add CustomProcessBaseUserRel"
// @Success 200 {object} model.CustomProcessBaseUserRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbaseuserrel [post]
// echo '{"base_id": 79,"user_id": 42}' | http POST "http://localhost:8080/customprocessbaseuserrel" X-Api-User:user123
func AddCustomProcessBaseUserRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	customprocessbaseuserrel := &model.CustomProcessBaseUserRel{}

	if err := readJSON(r, customprocessbaseuserrel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbaseuserrel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbaseuserrel.Prepare()

	if err := customprocessbaseuserrel.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_user_rel", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	customprocessbaseuserrel, _, err = dao.AddCustomProcessBaseUserRel(ctx, customprocessbaseuserrel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbaseuserrel)
}

// UpdateCustomProcessBaseUserRel Update a single record from custom_process_base_user_rel table in the custom_process database
// @Summary Update an record in table custom_process_base_user_rel
// @Description Update a single record from custom_process_base_user_rel table in the custom_process database
// @Tags CustomProcessBaseUserRel
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"// @Param  argUserID path int64 true "user_id"
// @Param  CustomProcessBaseUserRel body model.CustomProcessBaseUserRel true "Update CustomProcessBaseUserRel record"
// @Success 200 {object} model.CustomProcessBaseUserRel
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbaseuserrel/{argBaseID}/{argUserID} [put]
// echo '{"base_id": 79,"user_id": 42}' | http PUT "http://localhost:8080/customprocessbaseuserrel/1/1"  X-Api-User:user123
func UpdateCustomProcessBaseUserRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argUserID, err := parseInt64(ps, "argUserID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbaseuserrel := &model.CustomProcessBaseUserRel{}
	if err := readJSON(r, customprocessbaseuserrel); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbaseuserrel.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbaseuserrel.Prepare()

	if err := customprocessbaseuserrel.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_user_rel", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbaseuserrel, _, err = dao.UpdateCustomProcessBaseUserRel(ctx,
		argBaseID, argUserID,
		customprocessbaseuserrel)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbaseuserrel)
}

// DeleteCustomProcessBaseUserRel Delete a single record from custom_process_base_user_rel table in the custom_process database
// @Summary Delete a record from custom_process_base_user_rel
// @Description Delete a single record from custom_process_base_user_rel table in the custom_process database
// @Tags CustomProcessBaseUserRel
// @Accept  json
// @Produce  json
// @Param  argBaseID path int64 true "base_id"// @Param  argUserID path int64 true "user_id"
// @Success 204 {object} model.CustomProcessBaseUserRel
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /customprocessbaseuserrel/{argBaseID}/{argUserID} [delete]
// http DELETE "http://localhost:8080/customprocessbaseuserrel/1/1" X-Api-User:user123
func DeleteCustomProcessBaseUserRel(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argBaseID, err := parseUint64(ps, "argBaseID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	argUserID, err := parseInt64(ps, "argUserID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base_user_rel", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteCustomProcessBaseUserRel(ctx, argBaseID, argUserID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
